import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;

public abstract class ClipboardHandler {
	protected Properties env;
	protected Logger log;
	protected void setLogger(Logger aLogger) {
		log = aLogger;
	}
	protected void setEnvironment(Properties p) {
		env = p;
	}
    public String getText() {
        Clipboard sc = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable content = sc.getContents(null);
        if (content == null) return null;
        try {
        	return (String) content.getTransferData(DataFlavor.stringFlavor); 
        } catch (UnsupportedFlavorException ufe) {
		} catch (IOException ioe) { }
        return null;
    }
    public Image getImage() {
        Clipboard sc = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable content = sc.getContents(null);
        if (content == null) return null;
        try {
            return (Image) content.getTransferData(DataFlavor.imageFlavor);
        } catch (UnsupportedFlavorException ufe) {
        } catch (IOException ioe) { }
        return null;        
    }
    public DataFlavor[] getContentType() {
        Clipboard sc = Toolkit.getDefaultToolkit().getSystemClipboard();
        return sc.getAvailableDataFlavors();
    }
    public void setText(String content) {
        Clipboard sc = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable transText = new StringSelection(content);
        sc.setContents(transText, null);
    }
    static class ImageSelection implements Transferable {
        // the Image object which will be housed by the ImageSelection
        private Image image;
        public ImageSelection(Image image) {
            this.image = image;
        }
        // Returns the supported flavors of our implementation
        @Override
        public DataFlavor[] getTransferDataFlavors() {
          return new DataFlavor[] {DataFlavor.imageFlavor};
        }
        // Returns true if flavor is supported
        @Override
        public boolean isDataFlavorSupported(DataFlavor flavor) {
          return DataFlavor.imageFlavor.equals(flavor);
        }
        // Returns Image object housed by Transferable object
        @Override
        public Object getTransferData(DataFlavor flavor)
               throws UnsupportedFlavorException, IOException {
          if (!DataFlavor.imageFlavor.equals(flavor)) {
            throw new UnsupportedFlavorException(flavor);
          }
          return image;
        }
      }
    public void setImage(Image content) {
        Clipboard sc = Toolkit.getDefaultToolkit().getSystemClipboard();
        ImageSelection transImage = new ImageSelection(content);
        sc.setContents(transImage, null);        
    }
    public abstract boolean initialize();
	public abstract boolean processContent();
}
